package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.List;
import java.util.Set;


/**
 * @author Trellis
 * @since 2022/09/20 20:40
 */
@Service
public class ICartServiceImpl implements ICartService {
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    CartItemConverter cartItemConverter;



    @Override
    public List<CartProductDto> getCartListById(Long userId) {
        RMap<String, CartProductDto> map = redissonClient.getMap(String.valueOf(userId));
        List<CartProductDto> cartProductDtos = new ArrayList<>();
        try {
            Collection<CartProductDto> values = map.values();
            Object[] goods = values.toArray();
            for (Object good : goods) {
                cartProductDtos.add((CartProductDto) good);
            }
        }catch (Exception e){
            return cartProductDtos;
        }

        return cartProductDtos;
    }

    @Override
    public void addToCart(CartRequest request) {
        //根据userId获取map
        RMap<String, CartProductDto> map = redissonClient.getMap(String.valueOf(request.getUserId()));
        //根据商品id查商品表获取商品详细信息
        Item item = itemMapper.selectByPrimaryKey(request.getProductId());
        CartProductDto cartProductDto = cartItemConverter.item2Dto(item);
        cartProductDto.setProductNum(Long.valueOf(request.getProductNum()));
        cartProductDto.setChecked("true");

        //存入redis
        map.put(String.valueOf(request.getProductId()),cartProductDto);
    }

    @Override
    public void updateCartNum(CartRequest request) {
        //根据userId获取map
        RMap<String, CartProductDto> map = redissonClient.getMap(String.valueOf(request.getUserId()));
        //更改商品数量和是否选中状态
        CartProductDto cartProductDto = map.get(String.valueOf(request.getProductId()));
        cartProductDto.setProductNum(Long.valueOf(request.getProductNum()));
        cartProductDto.setChecked(request.getChecked());
        //存回
        map.put(String.valueOf(request.getProductId()),cartProductDto);
    }

    @Override
    public void checkAllCartItem(CheckAllItemRequest request) {
        RMap<Object, Object> map = redissonClient.getMap(request.getUserId().toString());
        Set<Object> objects = map.keySet();
        for (Object object : objects) {
            CartProductDto result = (CartProductDto) map.get(object);
            if (!(request.getChecked().equals(result.getChecked()))) {
                result.setChecked(request.getChecked());
                map.put(object,result);
            }
        }


    }

    @Override
    public void deleteCartItem(Long uid, Long itemId) {
        RMap<Object, Object> userMap = redissonClient.getMap(uid.toString());
        String key = itemId.toString();
        if (userMap.get(key) != null) {
            userMap.remove(key);
        }
    }

    @Override
    public void deleteCheckedItem(Long userId) {
        RMap<Object, Object> map = redissonClient.getMap(userId.toString());
        Set<Object> objects = map.keySet();
        for (Object object : objects) {
            CartProductDto result = (CartProductDto) map.get(object);
            if ("true".equals(result.getChecked())) {
                map.remove(object);
            }
        }


    }
}
